const query = require('./query')

const queryUserInfo = async (user_phone) => {
  const sql = 'select * from user where user_status = 1 and user_phone = ?'
  try {
    const result = await query(sql, user_phone)
    return result
  } catch (error) {
    return Promise.reject(error)
  }
}
const queryUserById = async (id) => {
  const sql = 'select * from user where id = ? and user_status = 1'
  try {
    const result = await query(sql, id)
    return result
  } catch (error) {
    return Promise.reject(error)
  }
}
const userLogin = async ({ user_phone, user_password }) => {
  const sql = 'select * from user where user_phone = ? and user_status = 1'
  try {
    const result = await query(sql, user_phone)
    if (result[0].user_password === user_password) {
      return '登陆成功！'
    } else {
      return Promise.reject('手机号或密码错误！')
    }
  } catch (error) {
    return Promise.reject(error)
  }
}
const userRegister = async ({ user_phone, user_password, user_name }) => {
  const sql = 'insert into user(user_phone,user_password,user_name,user_createTime) value (?,?,?,?)'
  try {
    const result = await query(sql, [user_phone, user_password, user_name, new Date()])
    return '注册成功！'
  } catch (error) {
    return Promise.reject(error)
  }

}

module.exports = {
  queryUserInfo,
  queryUserById,
  userLogin,
  userRegister
}